var work={
	dataAll:"",//总数据
	strAll:['<option value="" selected="true" disabled="true">请选择区域</option>','<option value="" selected="true" disabled="true">请选择楼号</option>','<option value="" selected="true" disabled="true">请选择单元</option>','<option value="" selected="true" disabled="true">请选择楼号</option>','<option value="" selected="true" disabled="true">请选择房间号</option>'],//初始select数据
	addressNum:1,//地址栏个数
	/*·····························初始化························*/
	init:function(){
		this.addressLink();
		this.eventListen();
		$("#tel").val(info.tel);
		// this.getSelect();

		// this.formsub();
	},
	/*·····························DOM························*/
	addressLink:function(){
		$.get("/index.php?m=Admin&c=Customer&a=read&id="+info.id,function(data){
			// console.log(data);
			info.customerData=data.data;
			for(var i=0;i<info.customerData.families.length;i++){
				var dataInfo=info.customerData.families[i];		
				work.addressInfo(.zone_id,);
			}
			work.getCommunityData();
		});
	
	},
	eventListen:function(){
		// 表单提交
		$(document).on("click","#formSub",function(){
			work.formsub();
		});
		// 地址栏目添加
		$(document).on("click","#addressAddCol",function(){
			var str=$("#addressTpl").html();
			$(".addressCol").append(str);
		});
		//    第一级
	    $(document).on("change",".xiaoqu",function () {
	    
	//如果下一级元素有大于2个option那么就重置后续select
	        // if($(".quyu").children('option').length>1){
	        //     $(this).nextAll().each(function(){
	        //         $(this).empty();
	        //     });
	        //    for(var i=0;i<strAll.length;i++){
	        //     $(this).nextAll().eq(i).html(strAll[i]);
	        //     } 
	        // }
	        info.xiaoquId = $(this).find("option:selected").val();
	        console.log(info.xiaoquId);
	        if(work.strLength(work.dataAll[info.xiaoquId].data)>0){
	            var zones=work.dataAll[info.xiaoquId].data;
	            console.log(zones);
	         	var id='quyu';
		        var data=zones;  
				var str="";
				var key='zone'
		        for (x in data){
		            if(data[x]['unit_id']==0){
		                console.log('无单元号');
		                 work.msg(this,'danyuan','单元');
		                 // break;
		            }
		            str+='<option value="'+data[x][key+'_id']+'">'+data[x][key+'_name']+'</option>';
		        }
                $(this).next("."+id).empty();
                $(this).next("."+id).append('<option value="" selected="true" disabled="true">请选择园区</option>');
		         $(this).next("."+id).append(str);
	            // work.strAdd(this,zones,'quyu','zone');
	        }else{
	             work.msg(this,'quyu','区域');
	             var build=work.dataAll[info.xiaoquId][0].data;
	             work.strAdd(this,build,'louhao');
	        }
	        
	    });
	    //    第二级
	    $(document).on("change",".quyu",function(){
	          info.quyuId = $(this).find("option:selected").val();
	        var build=work.dataAll[info.xiaoquId]['data'][info.quyuId].data;
        	var id='louhao';
	        var data=build;  
			var str="";
			var key='building'
	        for (x in data){
	            if(data[x]['unit_id']==0){
	                console.log('无单元号');
	                 work.msg(this,'danyuan','单元');
	                 // break;
	            }
	            str+='<option value="'+data[x][key+'_id']+'">'+data[x][key+'_name']+'</option>';
	        }
            $(this).next("."+id).empty();
            $(this).next("."+id).append('<option value="" selected="true" disabled="true">请选择楼号</option>');
	         $(this).next("."+id).append(str);
	        // work.strAdd(this,build,'louhao','building');
	    });
	    //    第三级
	   $(document).on("change",".louhao",function(){
	          info.louhaoId = $(this).find("option:selected").val();
	          var unit=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId].data;
	          console.log(unit);
		        var id='danyuan';
		        var data=unit;  
				var str="";
				var key='unit'
		        for (x in data){
		            if(data[x]['unit_id']==0){
		                console.log('无单元号');
		                 work.msg(this,'danyuan','单元');
		                 // break;
		            }
		            str+='<option value="'+data[x][key+'_id']+'">'+data[x][key+'_name']+'</option>';
		        }
	            $(this).next("."+id).empty();
	            $(this).next("."+id).append('<option value="" selected="true" disabled="true">请选择单元</option>');
		         $(this).next("."+id).append(str);
	          // work.strAdd(this,unit,'danyuan','unit');
	          for(x in unit){
	              if(unit[x]['unit_id']==0){
	                  $(".danyuan").find('option').eq(1).text('无单元');
	                  var room=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId]['data'][0].data;
		            var id='fangjian';
			        var data=room;  
					var str="";
					var key='room'
			        for (x in data){
			            if(data[x]['unit_id']==0){
			                console.log('无单元号');
			                 work.msg(this,'danyuan','单元');
			                 // break;
			            }
			            str+='<option value="'+data[x][key+'_id']+'">'+data[x][key+'_name']+'</option>';
			        }
		            $(this).next("."+id).empty();
		            $(this).next("."+id).append('<option value="" selected="true" disabled="true">请选择房间</option>');
			         $(this).next("."+id).append(str);
	                   // work.strAdd(this,room,'fangjian','room');
	              }
	          }
	    });
	        //    第四级
	    $(document).on("change",".danyuan",function(){
	         info.danyuanId = $(this).find("option:selected").val();
	         if(info.danyuanId){
	             var room=work.dataAll[info.xiaoquId]['data'][info.quyuId]['data'][info.louhaoId]['data'][info.danyuanId].data;
	            console.log(room);
	            var id='fangjian';
		        var data=room;  
				var str="";
				var key='room'
		        for (x in data){
		            if(data[x]['unit_id']==0){
		                console.log('无单元号');
		                 work.msg(this,'danyuan','单元');
		                 // break;
		            }
		            str+='<option value="'+data[x][key+'_id']+'">'+data[x][key+'_name']+'</option>';
		        }
	            $(this).next("."+id).empty();
	            $(this).next("."+id).append('<option value="" selected="true" disabled="true">请选择房间</option>');
		         $(this).next("."+id).append(str);
	            // work.strAdd(this,room,'fangjian','room');
	         }
	    });
	    //第五级修改后提交
	    $(document).on("change",".fangjian",function(){
	    	var roomId=$(this).find("option:selected").val();
	    	var unitId=$(this).parent().children(".danyuan").find("option:selected").val();
	    	var buidId=$(this).parent().children(".louhao").find("option:selected").val();
	    	var quyuId=$(this).parent().children(".quyu").find("option:selected").val();
	    	var communityId=$(this).parent().children(".xiaoqu").find("option:selected").val();
	    	var customerId=$(this).parent().children(".customerId").val();
	    	var familyId=$(this).parent().children(".familyId").val();
	    	if(familyId){
				 $.ajax({
			        url:"/index.php?m=Admin&c=Family&a=add",
			        type:"POST",
			        data:{
			            "room_id":roomId,
			            "unit_id":unitId,
			            "build_id":buidId,
			            "zone_id":quyuId,
			            "community_id":communityId,
			            "customer_id":customerId,
			            "family_id":familyId,
			        },
			        error:function(){
			            console.log("服务器繁忙");
			        },
			        success:function(data,status){
						console.log(data);
			           
			        }
			    });
	    	}else{
	    		$.ajax({
			        url:"/index.php?m=Admin&c=Family&a=update",
			        type:"POST",
			        data:{
			            "room_id":roomId,
			            "unit_id":unitId,
			            "build_id":buidId,
			            "zone_id":quyuId,
			            "community_id":communityId,
			            "customer_id":customerId,
			            "family_id":familyId,
			        },
			        error:function(){
			            console.log("服务器繁忙");
			        },
			        success:function(data,status){
						console.log(data);
			           
			        }
			    });
	    	}
	    });
			
	},
	/*·····························功能性函数························*/
	// 初始化生成下拉
	addressInfo:function(quyuId,,quyuName,buidId,buidName,unitId,unitName,roomId,roomName){
		if(quyuId&&buidId&&unitId&&roomId){
			$(".quyu").last().empty();
			$(".quyu").last().append('<option value="'+quyuId+'" selected="true">'+quyuName+'</option>');
			$(".louhao").last().empty();
			$(".louhao").last().append('<option value="'+buidId+'" selected="true">'+buidName+'</option>');
			$(".danyuan").last().empty();
			$(".danyuan").last().append('<option value="'+unitId+'" selected="true">'+buidName+'</option>');
			$(".fangjian").last().empty();
			$(".fangjian").last().append('<option value="'+roomName+'" selected="true">'+roomName+'</option>');
		}else{
			$(".quyu").last().empty();
			$(".quyu").last().append('<option value="" selected="true">"请选择区域"</option>');
			$(".louhao").last().empty();
			$(".louhao").last().append('<option value="" selected="true">"请选择楼号"</option>');
			$(".danyuan").last().empty();
			$(".danyuan").last().append('<option value="" selected="true">"请选择单元"</option>');
			$(".fangjian").last().empty();
			$(".fangjian").last().append('<option value="" selected="true">"请选择房间"</option>');
		}
		
		var str=$("#addressTpl").html();
		$(".addressCol").append(str);		
	}
	// 五级联动
	//获取数据并设置小区下拉
	getCommunityData:function(){
		 $.get("/index.php?m=Admin&c=House&a=json",function(data){
            // var data=$.parseJSON(data);
            work.dataAll=data.data;
            data=data.data;
            // console.log(data);
            var str="";
            var zoneStr="";
            var orignData=info.customerData.families;
            for(var i=0;i<orignData.length;i++){
            	for(x in data){
	                if(data[x]['community_name']){
	                	if(data[x]['community_id']==orignData[i].community_id){
	                		str+='<option value="'+data[x]['community_id']+'" selected="true">'+data[x]['community_name']+'</option>';
	                	}else{
	                		str+='<option value="'+data[x]['community_id']+'">'+data[x]['community_name']+'</option>';
	                	}

	                }
	            }
	            $(".xiaoqu").append(str);
            }
           
        });
	},
	// 获取初始select的值
	getSelect:function(){
		 $(".xiaoqu").nextAll().each(function(){
            work.strAll.push($(this).html());
            });
	},
	 // 计算数组或对象的长度
    strLength:function(str){
        t=typeof(str);
        if(t=='string'){
            return str.length;
        }else if(t=='object'){
            var n=0;
            for(var i in str){
                n++
            }
            return n;
        }
     },
      // 下拉菜单字符串生成
    // data为数据，id为id索引值
    strAdd:function(obj,data,id,key){
        var str="";
        for (x in data){
            if(data[x]['unit_id']==0){
                console.log('无单元号');
                 work.msg(this,'danyuan','单元');
                 // break;
            }
            str+='<option value="'+data[x][key+'_id']+'">'+data[x][key+'_name']+'</option>';
        }
        switch(id){
            case 'xiaoqu':
                $(obj).next("."+id).empty();
                 $(obj).next("."+id).append('<option value="" selected="true" disabled="true">请选择小区</option>');
                break;
            case 'quyu':
                 $(obj).next("."+id).empty();
                 $(obj).next("."+id).append('<option value="" selected="true" disabled="true">请选择区域</option>');
                break;
            case 'louhao':
                 $(obj).next("."+id).empty();
                $(obj).next("."+id).append('<option value="" selected="true" disabled="true">请选择楼号</option>');
                break;
            case 'danyuan':
                 $(obj).next("."+id).empty();
                 $(obj).next("."+id).append('<option value="" selected="true" disabled="true">请选择单元</option>');
                break;
            case 'fangjian':
                 $(obj).next("."+id).empty();
                 $(obj).next("."+id).append('<option value="" selected="true" disabled="true">请选择房间号</option>');
                break;
        }
         $(obj).next("."+id).append(str);
    },
      // 没有下一级属性时显示的信息
    msg:function(obj,id,msg){
        $(obj).next("."+id).find("option").text('无'+msg);
    },
	/*·····························插件初始化························*/
	formsub:function(){
		        $("#form-customer-add").ajaxSubmit({
		        type:'POST',
		        url:'/index.php?m=Admin&c=Customer&a=update',
		        success:function(data){
		          console.log(status);
		        	if(data.stat==1){
		              layer.msg("修改成功");
		              var index = parent.layer.getFrameIndex(window.name);
		              setTimeout(function(){
		                   parent.layer.close(index);
		              },1000);
		              // parent.location.reload();
		            parent.window.location.href="/index.php?m=Service&c=Center&a=index&tel="+info.tel;
		          }else{
		            layer.msg(data.msg);
		          }
		          }
		        });
	}
}